package patrol.mobile.com.mobilepatrol.activity.office;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.OtherBusinessSearchAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.OtherBusinessBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

import static patrol.mobile.com.mobilepatrol.activity.CommonListActivity.ROWS;

/**
 * 其他业务查询
 */
public class OtherBusinessSearchActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private OtherBusinessSearchAdapter mAdapter;
    private int page = 1;
    private List<OtherBusinessBean> otherBusinessBeans;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_other_business_search;
    }

    @Override
    public void initView() {
        super.initView();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CommonUtil.exitActivityAndBackAnim(OtherBusinessSearchActivity.this, true);
                    }
                });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("其他业务查询");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void initData() {
        super.initData();
        mAdapter = new OtherBusinessSearchAdapter(this);
        recyclerView.setAdapter(mAdapter);
        requestData(true, "", "", "", "", "");
    }

    private void requestData(final boolean isRefresh, String title, String orgId, String startDate, String endDate, String isFinish) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.TASK_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("rows", ROWS + "");
        map.put("orgId", orgId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("title", title);
        map.put("is_finish", isFinish);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                //setdata
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();

                JsonArray listArray = jo.getAsJsonArray("list");
                int rows = jo.get("rows").getAsInt();
                otherBusinessBeans = GsonUtil.jsonToList(listArray.toString(), OtherBusinessBean.class);
                setData(mAdapter, page, isRefresh, otherBusinessBeans, rows);
            }
        });
    }

    @Override
    public void setListener() {
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(OtherBusinessSearchActivity.this, OtherOfficeDetailActivity.class);
                intent.putExtra(OtherOfficeDetailActivity.ID, otherBusinessBeans.get(position).getId());
                intent.putExtra("delete",1);
                startActivity(intent);
            }
        });

        findViewById(R.id.top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(OtherBusinessSearchActivity.this, OtherBusinessSearchAllActivity.class), 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            Bundle result = data.getExtras().getBundle("result");
            String currentPosition = result.getString("currentPosition");
            String currentOrgId = result.getString("currentOrgId");
            String currentStartTime = result.getString("currentStartTime");
            String currentEndTime = result.getString("currentEndTime");
            String searchText = result.getString("searchText");
            Log.i("business_result", "onActivityResult: " + currentPosition + "--" + currentOrgId + "--" + currentStartTime + "--" + currentEndTime + "--" + searchText);
            requestData(true, searchText, currentOrgId, currentStartTime, currentEndTime, currentPosition);
        }
    }
}
